package com.nsoft.server.service.main;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ConfigurableApplicationContext;

import com.nsoft.server.serviceImpl.FacadeFactoryImpl;
import com.nsoft.thickclient.trackMove.intr.party.ThinParty;

/**
 * Main entry class of the server. The command-line call will call the main
 * method, which will simply load the Spring Application Context. this context
 * itself contains a singleton of this server class.
 * 
 * @author gelezovi041107
 * 
 */
public class CPMServer implements InitializingBean {
	/** the application context. */
	private static ConfigurableApplicationContext springContext;
	/** the server context. */
	// private static IServerContext context;
	/** The server version. */
	private String version;
	/** The application Name. */
	private String name;

	 private static final Logger LOGGER = Logger.getLogger(CPMServer.class);

	/**
	 * Starts or stop the server.
	 * 
	 * @param args
	 *            command line arguments : start or stop
	 * @throws DCMException
	 */
	public static void main(String[] args) {

	/*	GCService service =FacadeFactoryImpl.getGCService();
		service.saveGc(new GcHeader());
		*/
		
		List<ThinParty> list=FacadeFactoryImpl.getPartyService().getAllThinParties(new ThinParty(10,"prav*"));
		System.out.println(list.size());
		/*UserService service = FacadeFactoryImpl.getUserService();
		User user = new User();
		user.setName("satya");
		user.setPassword("bycycle");
		user.setLogin("praveen");
		user.setRole("Admin");
		user.setCreationDate(new Date());
		user = service.saveUser(user);
		System.out.println(user.getLogin());

		PartyService partyService = FacadeFactoryImpl.getPartyService();
		Party party = new Party();
		party.setContactPerson("abc");
		party.setContactPhone("9880314919");
		party.setCrteationDate(new Date());
		party.setName("Praveen");
		party.setTan("fljlf");
		party.setPan("fbfv");
		party.setTin("jjdfvjd");
		party.setRemarks("test");
		party.setExim("dndsj");
		party = partyService.saveParty(party);
		System.out.println(party.getPan());
		

		VechileService vechileService = FacadeFactoryImpl.getVechileService();
		Vehicle vehicle = new Vehicle();
		vehicle.setBillingPartyId(1L);
		vehicle.setInsurence("insurance");
		vehicle.setLastDriverLicence("license");
		vehicle.setLastDriverName("abc");
		vehicle.setInsurenceExpiryDate(new Date());
		vehicle.setOwner("SRMT");
		vehicle.setRegistrationNo("kdbg");
		vehicle.setRemarks("test");
		vehicle.setRegistrationDate(new Date());
		vehicle.setMake("fj");
		vehicle = vechileService.saveVechile(vehicle);
		System.out.println(vehicle.getLastDriverLicence());

		AccountService accountService = FacadeFactoryImpl.getAccountService();
		Account account = new Account();
		account.setCreationDate(new Date());
		account.setCurrentBalance(Double.MIN_VALUE);
		account.setNumber("12345");
		account.setPartyId(1L);
		account.setName("praveen");
		account.setOpenDate(new Date());
		account.setUpdationDate(new Date());
		account.setLimits(new Double("5456448"));
		account.setSelectable(Boolean.TRUE);
		account = accountService.saveAccount(account);
		System.out.println(account.getRemarks());*/
		
		
	}

	/**
	 * Use this method (from Spring) to launch every init-time initialization.
	 * In our example, we will work on the static context of the server
	 * {@inheritDoc}
	 */
	public void afterPropertiesSet() {
		initVersion();
		/*
		 * IStaticContext staticContext = new StaticContext();
		 * staticContext.setApplicationName(getName());
		 * staticContext.setVersion(getVersion());
		 * context.setStaticContext(staticContext);
		 * LOGGER.info("Server context configured");
		 */
	}

	/**
	 * Init the version number (stored in the jar).
	 * 
	 * @note This method should be duplicated in your application code, because
	 *       it should stay in a jar for which you can provide a version number
	 */
	public void initVersion() {
		this.version = CPMServer.class.getPackage().getImplementationVersion();
		if (this.version == null) {
			this.version = "SNAPSHOT"; // If not packaged into a jar (e.g.
			// Eclipse)
		}
	}

	/**
	 * Returns the version.
	 * 
	 * @return the version.
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * Set the version of the server.
	 * 
	 * @param version
	 *            to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}

	/**
	 * Returns the Spring context.
	 * 
	 * @return springContext the context Spring.
	 */
	public static ConfigurableApplicationContext getSpringContext() {
		return springContext;
	}

	/**
	 * Sets the Spring context.
	 * 
	 * @param springContext
	 *            the Spring context to set.
	 */
	public static void setSpringContext(
			ConfigurableApplicationContext springContext) {
		CPMServer.springContext = springContext;
	}

	/**
	 * Returns the Sever Name.
	 * 
	 * @return serverName
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Sets the server Name.
	 * 
	 * @param server
	 *            name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
}
